package pl.edu.wsinf.lukaszwalczak.emailclient;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pl.edu.wsinf.lukaszwalczak.emailclient.controller.MainWindowController;

/*
 * To jest klasa która startuje aplikacje. Dziedziczy ona po Application która
 * jest bazową klasą dla programów napisanych w JavaFX
 */
public class EmailClient extends Application {

    @Override
    public void start(Stage primaryStage) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EmailClientSpringConfig.class);
        final MainWindowController mainWindowController = context.getBean(MainWindowController.class);
        mainWindowController.setPrimaryStage(primaryStage);

        Scene scene = new Scene(mainWindowController.getView(), 1000, 600, Color.WHITESMOKE);
        scene.getStylesheets().add(EmailClient.class.getResource("/css/stylesheet.css").toExternalForm());

        primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                mainWindowController.onPrimaryStageShow();
            }
        });

        primaryStage.setTitle("EmailClient");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application. main() serves only as fallback in case the
     * application can not be launched through deployment artifacts, e.g., in IDEs with limited FX support. NetBeans
     * ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
